package ssf.app.action;

import java.util.Map;

import javax.servlet.http.*;

import org.apache.struts.action.*;

import ssf.core.common.PagingList;
import ssf.core.util.MapUtil;

public class UserAction extends BaseAction {
	/**
	 * 默认执行方法
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String action = request.getParameter("action");
		if (action == null) {
			return other(mapping, form, request, response);
		} else if ("list".equals(action)) {
			return list(mapping, form, request, response);
		} else if ("add".equals(action)) {
			return add(mapping, form, request, response);
		} else {
			return other(mapping, form, request, response);
		}
	}

	/**
	 * 获取用户列表
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		PagingList userList = getServMgr().getUserService()
				.getAllUsers(request);
		request.setAttribute("userList", userList);
		// 参数名必须是URL，action=list根据需要改写
		request.setAttribute("URL", request.getRequestURI() + "?action=list");
		return mapping.findForward("userList");
	}

	/**
	 * 添加用户
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// test
		addMessage(request, "OK");
		addRedirURL(request, "ok", "index.do");
		return mapping.findForward(EXECUTE_RESULT);
	}

	public ActionForward other(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		setResult(request, "ERROR");
		addMessage(request, "未定义操作，请返回!");
		addRedirURL(request, "返回", "@back");
		return mapping.findForward(EXECUTE_RESULT);
	}
}
